package cn.com.yiqi.wsc.service.role.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.func.FunctionDao;
import cn.com.yiqi.wsc.dao.role.RoleDao;
import cn.com.yiqi.wsc.dao.role.RoleFuncDao;
import cn.com.yiqi.wsc.dao.role.criteria.RoleCriteria;
import cn.com.yiqi.wsc.entity.func.Function;
import cn.com.yiqi.wsc.entity.role.Role;
import cn.com.yiqi.wsc.entity.role.RoleFunc;
import cn.com.yiqi.wsc.service.role.RoleService;
import cn.com.yiqi.wsc.service.role.dataobject.RoleDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 何刚
 * @ClassName: ${class}
 * @Description: TODO
 * @date 2017/2/27
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleFuncDao roleFuncDao;

    @Autowired
    private FunctionDao functionDao;

    @Override
    public Result pageList(RoleCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());

        PageHelper.orderBy("d_insert_time DESC");
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleDao.pageList(criteria));

        result.putData("list", rolePageInfo.getList());
        result.putData("total", rolePageInfo.getTotal());

        return result;
    }

    @Transactional
    @Override
    public Result insert(RoleDO role) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        role.setIsAll(0);
        roleDao.insert(role);

        List<Integer> functionIds = role.getFunctionIds();

        List<RoleFunc> functions = new ArrayList<>();

        Integer roleId = role.getId();
        wrapperRoleFunc(functionIds, functions, roleId);
        if (!functions.isEmpty()) {
            //批量插入
            roleFuncDao.batchInsert(functions);
        }


        return result;
    }

    @Transactional
    @Override
    public Result update(RoleDO role) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        roleDao.update(role);

        List<Integer> functionIds = role.getFunctionIds();
        List<RoleFunc> functions = new ArrayList<>();
        Integer roleId = role.getId();
        wrapperRoleFunc(functionIds, functions, roleId);

        if (!functions.isEmpty()) {
            //如果做了修改删除之前的
            roleFuncDao.deleteByRoleId(roleId);
            //批量插入
            roleFuncDao.batchInsert(functions);
        }


        return result;
    }

    private void wrapperRoleFunc(List<Integer> functionIds, List<RoleFunc> functions, Integer roleId) {
        if (functionIds != null && !functionIds.isEmpty()) {
            for (Integer id : functionIds) {
                RoleFunc roleFunc = new RoleFunc();
                roleFunc.setRoleId(roleId);
                roleFunc.setFuncId(id);
                functions.add(roleFunc);
            }
        }
    }


    @Override
    public Result delete(Role role) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        roleDao.delete(role);
        return result;
    }

    @Override
    public Result detail(Role role) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        List<Function> functions = functionDao.getFunctionByRoleId(role.getId());

        role = roleDao.get(role);

        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(role, roleDO);

        List<Integer> functionIds = new ArrayList<>();

        if (!CollectionUtils.isEmpty(functions)) {
            for (Function function : functions) {
                functionIds.add(function.getId());
            }
        }


        roleDO.setFunctionIds(functionIds);

        result.putData("detail", roleDO);
        result.putData("functionList", functions);


        return result;
    }
}
